package com.mrent.web.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrent.entity.Member;
import com.mrent.service.MemberService;
import com.mrent.service.Page;
import com.mrent.service.PictureService;
import com.mrent.util.Constance;
import com.mrent.util.RenderUtil;

@Controller
public class AdminMemberController {
	
	@Autowired
	private MemberService memberService;
	
	@Autowired
	private PictureService pictureService;
	
	/**
	 * 会员管理.
	 * 
	 * @return
	 */
	@RequestMapping("/admin/member/index")
	public String index(){
		
		return "admin/member/home";
	}
	
	/**
	 * 会员列表.
	 * 
	 * @param response
	 * @return
	 */
	@RequestMapping("/admin/member/list")
	public String list(String page,HttpServletResponse response){
		
		int p = 1;
		Page pp = new Page();
		try{
			p = Integer.parseInt(page);
		}catch(Exception e){
			p = 1;
		}
		pp.setPageNo(p);
		pp.setPageSize(Constance.DEFAULT_PAGE_SIZE);
		
		List<Member> list = memberService.listMember(pp);
		
		Map<String,Object> json = new HashMap<String,Object>();
		json.put("rows", list);
		json.put("total", pp.getTotalCount());
		RenderUtil.renderJson(json,response);
		
		return null;
	}
	
	/**
	 * 查看会员.
	 * 
	 * @param memberId
	 * @param model
	 * @return
	 */
	@RequestMapping("/admin/member/{memberId}")
	public String view(@PathVariable Long memberId,ModelMap model){
		Member member = memberService.getMember(memberId);
		if(member != null ){
			model.addAttribute("member", member);
		}
		return "admin/member/view";		
	}
	
	/**
	 * 修改会员.
	 * 
	 * @param memberId
	 * @param response
	 * @return
	 */
	@RequestMapping("/admin/member/modify/{memberId}")
	public String modify(@PathVariable Long memberId,Integer state,HttpServletResponse response){
		Member member = memberService.getMember(memberId);
		if(member != null){
			member.setMemberState(state);
		}
		memberService.modifyMember(member);
		
		Map<String,Object> json = new HashMap<String,Object>();
		json.put("result", "Success");
		RenderUtil.renderJson(json, response);
		return null;
	}
	
	/**
	 * 会员删除.
	 * 
	 * @param memberId
	 * @return
	 */
	@RequestMapping("/admin/member/del/{memberId}")
	public String del(@PathVariable Long memberId,HttpServletResponse response){
		Member member = memberService.getMember(memberId);
		if(member != null){
			pictureService.delPicture(member.getMemberPhoto());
		}
		memberService.delMember(memberId);
		
		Map<String,Object> json = new HashMap<String,Object>();
		json.put("result", "Success");
		RenderUtil.renderJson(json, response);
		return null;
	}

}
